
from entity.Double_Color_Ball import Double_Color_Ball;
from util.sqlalchemy_util import *;
import pandas as pd;
from pandas import Series, DataFrame;
from pyecharts.faker import Faker;
from pyecharts import options as ops;
from pyecharts.charts import Line, Line3D, Bar, Pie;
from datetime import datetime;

'''
双色球数据处理
'''

# 双色球数据存储
def save_dcbs_in_db(dcb_list=[]):
    for dcb in dcb_list:
        insert_(dcb, "issue_no");

# 查询所有数据，并封装到df数据中
def init_dcb_df():
    index_list = [];
    column_list = ["开奖日期", "红球", "蓝球"];
    data_list = [];
    dcb_list = get_all(Double_Color_Ball());
    for dcb in dcb_list:
        index_list.append(dcb.issue_no);
        line_list = [];
        line_list.append(dcb.aware_date);
        line_list.append(dcb.red_ball);
        line_list.append(dcb.blue_ball);
        data_list.append(line_list);

    df = DataFrame(data=data_list, index=index_list, columns=column_list)\
        .sort_index(axis=0, ascending=True);
    df = pd.concat([
        df["开奖日期"],
        df["红球"].str.split(" ", expand=True)
            .rename(columns={0: "红球1", 1: "红球2", 2: "红球3", 3:"红球4", 4:"红球5", 5:"红球6", 6:"红球7"}),
        df["蓝球"]
    ], axis=1);
    return df;

def dcb_line(df):
    pass
    x_list = df["开奖日期"][-40:].apply(lambda item:item.strftime("%Y-%m-%d")).values.tolist();
    Line().add_xaxis(
        df["开奖日期"][-40:].apply(lambda item:item.strftime("%Y-%m-%d")).values.tolist()
    ).add_yaxis(
        series_name="红球1",
        y_axis=df["红球1"][-40:].apply(lambda item:int(item)).values.tolist(),
        itemstyle_opts=ops.ItemStyleOpts(color=Faker.rand_color())
    ).add_yaxis(
        series_name="红球2",
        y_axis=df["红球2"][-40:].apply(lambda item:int(item)).values.tolist(),
        itemstyle_opts=ops.ItemStyleOpts(color=Faker.rand_color())
    ).add_yaxis(
        series_name="红球3",
        y_axis=df["红球3"][-40:].apply(lambda item:int(item)).values.tolist(),
        itemstyle_opts=ops.ItemStyleOpts(color=Faker.rand_color())
    ).add_yaxis(
        series_name="红球4",
        y_axis=df["红球4"][-40:].apply(lambda item:int(item)).values.tolist(),
        itemstyle_opts=ops.ItemStyleOpts(color=Faker.rand_color())
    ).add_yaxis(
        series_name="红球5",
        y_axis=df["红球5"][-40:].apply(lambda item:int(item)).values.tolist(),
        itemstyle_opts=ops.ItemStyleOpts(color=Faker.rand_color())
    ).add_yaxis(
        series_name="红球6",
        y_axis=df["红球6"][-40:].apply(lambda item:int(item)).values.tolist(),
        itemstyle_opts=ops.ItemStyleOpts(color=Faker.rand_color())
    ).add_yaxis(
        series_name="蓝球",
        y_axis=df["蓝球"][-40:].apply(lambda item:int(item)).values.tolist(),
        itemstyle_opts=ops.ItemStyleOpts(color=Faker.rand_color())
    ).set_global_opts(
        title_opts=ops.TitleOpts(title="双色球折线图", subtitle="副标题", pos_left="10%")
    ).render(
        path="/temp/dcb_line.html"
    );

if __name__ == '__main__':
    df = init_dcb_df();
    dcb_line(df);